//
//  EpointScrollTap.m
//  EpointFrame5
//
//  Created by wangzhigang on 14-5-16.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "EpointScrollTap.h"
#import "UIView+Addtions.h"

#define ITEM_HEIGHT 30
#define TAP_SCROLLVIEW_SINGLE_WIDTH 120


@implementation EpointScrollTap

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

-(id)initWithFrame:(CGRect)frame :(NSArray*)titles{
    self = [super initWithFrame:CGRectMake(0, 0, TAP_SCROLLVIEW_SINGLE_WIDTH*titles.count, ITEM_HEIGHT)];
    if (self) {
        // Initialization code
        epointTitles = titles;
        bgView = [[UIButton alloc] initWithFrame:CGRectMake(0,(self.height-ITEM_HEIGHT)/2,TAP_SCROLLVIEW_SINGLE_WIDTH,ITEM_HEIGHT)];
        bgView.backgroundColor = COLOR(24, 128, 243);
        bgView.layer.cornerRadius = 16;
        [self addSubview:bgView];
        [self drawChildrenView];
    }
    return self;
}

-(void)drawChildrenView{
    for (int i=0; i<epointTitles.count; i++) {
        NSString* title =  [epointTitles objectAtIndex:i];
        UIButton* titleUb = [[UIButton alloc] initWithFrame:CGRectMake(TAP_SCROLLVIEW_SINGLE_WIDTH*i, (self.height-ITEM_HEIGHT)/2, TAP_SCROLLVIEW_SINGLE_WIDTH, ITEM_HEIGHT)];
        titleUb.titleLabel.font = [UIFont fontWithName:@"Arial" size:18];
        [titleUb setTitle:title forState:UIControlStateNormal];
        
        titleUb.backgroundColor=[UIColor clearColor];
        [titleUb setTag:i];
        [titleUb addTarget:self action:@selector(slideAnim:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:titleUb];
    }
    
}

-(void)slideAnim:(id)sender{
    UIButton* btn = (UIButton*)sender;
    [UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:0.5];
    bgView.left = btn.left;
    [UIView commitAnimations];
    if(self.delegate!=nil){
        [self.delegate selectIndexTitle_Pad:btn.tag];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
